//
// Created by HP on 2025/11/17.
//

#include "TicketOperation.h"
#include <iostream>

const std::string TicketOperationType::buyTicket = "buyTicket";
const std::string TicketOperationType::sellTicket = "sellTicket";
const std::string TicketOperationType::refundTicket = "refundTicket";

TicketOperation::TicketOperation() : Model("ticket_operations") {
    this->operationType = TicketOperationType::buyTicket;
    setTableDefault("userId", "INT NOT NULL");
    setTableDefault("ticketId", "INT NOT NULL");
    setTableDefault("operationType", "VARCHAR(50) NOT NULL");
    setTableDefault("quantity", "INT NOT NULL DEFAULT 1");
    setTableDefault("price", "VARCHAR(50) NOT NULL");
    setTableDefault("createdAt", "DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP");
}

void TicketOperation::bind(const std::string& key, const std::string& value) {
    if (!TableCols.count(key)) {
        std::cerr << "Error: Invalid key for ticket operation bind" << std::endl;
        return;
    }
    if (TableCols[key].find("UNIQUE") == std::string::npos && key != "id") {
        std::cerr << "Error: Key is not unique for ticket operation bind" << std::endl;
        return;
    }

    std::string selectSql = "SELECT * FROM " + TableName + " WHERE " + key + " = '" + Model::escapeSqlLiteral(value) + "';";
    std::vector<std::map<std::string, std::string>> result = DBConnector::getInstance()->query(selectSql);

    if (result.empty()) {
        std::cerr << "bind failed: SQL execution error or no result. SQL: " << selectSql << std::endl;
        return;
    }

    std::cout << "bind success. Table: " << TableName << std::endl;

    if (result.size() > 1) {
        std::cerr << "Error: Multiple ticket operations found with the same " << key << std::endl;
        return;
    } else if (result.size() == 1) {
        this->id = result[0]["id"];
        this->userId = result[0]["userId"];
        this->ticketId = result[0]["ticketId"];
        this->operationType = result[0]["operationType"];
        this->quantity = result[0]["quantity"];
        this->price = result[0]["price"];
        this->createdAt = result[0]["createdAt"];
    }
}

bool TicketOperation::save() {
    std::string createOperationSql = "INSERT INTO " + TableName + " (userId, ticketId, operationType, quantity, price) VALUES (" 
                                     + userId + ", " 
                                     + ticketId + ", '" 
                                     + Model::escapeSqlLiteral(operationType) + "', " 
                                     + quantity + ", '" 
                                     + Model::escapeSqlLiteral(price) + "');";
    int executeCode = DBConnector::getInstance()->execute(createOperationSql);
    if (executeCode == -1) {
        std::cerr << "create ticket operation failed: SQL execution error. SQL: " << createOperationSql << std::endl;
        return false;
    } else {
        std::cout << "create ticket operation success. Table: " << TableName << std::endl;
        return true;
    }
}
